<template>
  <el-breadcrumb bd-E9F1FF text-18px p-l-7px :separator-icon="ArrowRight" class="flex-y-center">
    <div flex border-b-3px border-b-solid h-44px border-b="[#1684FC]">
      <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
        <div
          v-if="item.redirect === 'noredirect' || index === breadcrumbs.length - 1"
          flex
          class="color-gray-400"
        >
          <!-- <div color-1684FC text-18px self-center mr-10px :class="`i-svg:${item.meta?.icon}`" /> -->

          <!-- 菜单图标 -->
          <template v-if="item.meta?.icon && index === 0">
            <el-icon
              v-if="isElIcon(item.meta?.icon)"
              color-1684FC
              self-center
              text-18px
              mr-10px
              class="el-icon"
            >
              <component :is="iconComponent(item.meta?.icon)" />
            </el-icon>
            <div
              v-else
              color="#1684FC"
              self-center
              text-18px
              mr-10px
              :class="`i-svg:${item.meta?.icon}`"
            />
          </template>
          <span color-1684FC text-18px>
            {{ translateRouteTitle(item.meta.title) }}
          </span>
        </div>
        <a v-else flex @click.prevent="handleLink(item)">
          <!-- <div color-1684FC self-center text-18px mr-10px :class="`i-svg:${item.meta?.icon}`" /> -->
          <!-- 菜单图标 -->
          <template v-if="item.meta?.icon && index === 0">
            <el-icon
              v-if="isElIcon(item.meta?.icon)"
              color="#1684FC"
              self-center
              text-18px
              mr-10px
              class="el-icon"
            >
              <component :is="iconComponent(item.meta?.icon)" />
            </el-icon>
            <div
              v-else
              color-1684FC
              self-center
              text-18px
              mr-10px
              :class="`i-svg:${item.meta?.icon}`"
            />
          </template>
          <span color-1684FC text-18px>
            {{ translateRouteTitle(item.meta.title) }}
          </span>
        </a>
      </el-breadcrumb-item>
    </div>
  </el-breadcrumb>
</template>

<script setup lang="ts">
import { RouteLocationMatched } from "vue-router";
import { compile } from "path-to-regexp";
import { ArrowRight } from "@element-plus/icons-vue";
import router from "@/router";
import { translateRouteTitle } from "@/utils/i18n";

const currentRoute = useRoute();
const pathCompile = (path: string) => {
  const { params } = currentRoute;
  const toPath = compile(path);
  return toPath(params);
};

const breadcrumbs = ref<Array<RouteLocationMatched>>([]);

function isElIcon(icon: string) {
  return icon.startsWith("el-icon");
}
function iconComponent(icon: string) {
  return icon.replace("el-icon-", "");
}

function getBreadcrumb() {
  let matched = currentRoute.matched.filter((item) => item.meta && item.meta.title);
  const first = matched[0];
  if (!isDashboard(first)) {
    matched = [
      { path: "/dashboard", meta: { title: "dashboard", breadcrumb: false } } as any,
    ].concat(matched);
  }
  breadcrumbs.value = matched.filter((item) => {
    return item.meta && item.meta.title && item.meta.breadcrumb !== false;
  });
  console.log("Breadcrumb mounted", breadcrumbs.value);
}

function isDashboard(route: RouteLocationMatched) {
  const name = route && route.name;
  if (!name) {
    return false;
  }
  return name.toString().trim().toLocaleLowerCase() === "Dashboard".toLocaleLowerCase();
}

function handleLink(item: any) {
  const { redirect, path } = item;
  if (redirect) {
    router.push(redirect).catch((err) => {
      console.warn(err);
    });
    return;
  }
  router.push(pathCompile(path)).catch((err) => {
    console.warn(err);
  });
}

watch(
  () => currentRoute.path,
  (path) => {
    if (path.startsWith("/redirect/")) {
      return;
    }
    getBreadcrumb();
  }
);

onBeforeMount(() => {
  getBreadcrumb();
  console.log("Breadcrumb mounted", breadcrumbs.value);
});
</script>

<style lang="scss" scoped>
// 覆盖 element-plus 的样式
.el-breadcrumb__inner,
.el-breadcrumb__inner a {
  font-weight: 400 !important;
}
</style>
